﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.LogDomain;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Domain.ProcessModuleDomain;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{
    /// <summary>
    /// 清除WCS-PLC数据
    /// </summary>
    public class ClearDataService : IPlanarGeneralPlcActionProcessService
    {
        private readonly ILogUtil _LogUtil;
        private readonly IPlcConnectionPool _PlcConnectionPool;
        private readonly IPlcWriteDbInfoRepository _PlcWriteDbInfoRepository;
        private readonly IRunLogInfoService _IRunLogInfoService;

        public ClearDataService(ILogUtil logUtil
            , IPlcConnectionPool plcConnectionPool
            , IPlcWriteDbInfoRepository plcWriteDbInfoRepository
             , IRunLogInfoService runLogInfoService)
        {
            _LogUtil = logUtil;
            _PlcConnectionPool = plcConnectionPool;
            _PlcWriteDbInfoRepository = plcWriteDbInfoRepository;
            _IRunLogInfoService = runLogInfoService;
        }
        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, BytesObjectBase wcsActionMessage, EquipmentDbObject planarEquipmentDbObject/*, BytesObjectBase plcStateMassge*/)
        {
            /*
             * 处理逻辑
             * 组织plc报文，写入plc(plc什么握手则回什么握手)
             */

            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBO;//Plc的动作报文
            var wcsMessage = wcsActionMessage as PlanarGeneralWcsActionBO;//Wcs的动作报文
            RunLogInfo runLogInfo = new RunLogInfo();
            runLogInfo.ContainerBarcode = plcMessage.PalletBarcode;
            runLogInfo.EquipmentCode = plcMessage.EquipmentCode;
            runLogInfo.RunType = "清除WCS-PLC数据";

            //runLogInfo.RunLog = "清除WCS-PLC数据任务开始";
            //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

            //组织plc报文，写入plc(plc什么握手则回什么握手)
            var objectType = typeof(PlanarGeneralWriteWcsAction).Name;
            var writeConfig = await _PlcWriteDbInfoRepository.GetFirstAsync(it => it.EquipmentCode == plcMessage.EquipmentCode && it.ObjectType == objectType);
            if (writeConfig == null)
            {
                runLogInfo.RunLog = "获取PLC写入配置失败";
                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                _LogUtil.Error(runLogInfo.RunLog);

                return;
            }

            //plc报文什么握手类型则回什么握手类型(清除平面设备数据)
            PlanarGeneralWriteWcsAction writeBo = new PlanarGeneralWriteWcsAction(
                writeConfig.PlcConnectionId,
                writeConfig.ID,
                writeConfig.DbCode,
                writeConfig.DbStart,
                objectType,
                plcMessage.EquipmentCode.ToInt32(),
                0,//plcMessage.ActionType
                0, //plcMessage.ToEquipmentCode,
                0, //plcMessage.TaskCode,
                "", //plcMessage.PalletBarcode,
                0,
                0//plcMessage.Alrm
                   );

            var plcConn = _PlcConnectionPool.S7GetWritePlcConnection(writeBo.PlcConnectionID);
            if (plcConn == null)
            {
                runLogInfo.RunLog =$"获取连接({writeBo.PlcConnectionID})失败";
                _LogUtil.Error (runLogInfo.RunLog);
                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                return;
            }
            //if (plcMessage.EquipmentCode == "320662")
            //{
            //    _LogUtil.Debug($"{DateTime.Now}：挑选位【{plcMessage.EquipmentCode}】开始清理数据！");
            //}
            var writeResult = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
            if (writeResult == false)
            {
                runLogInfo.RunLog = $"写入报文失败";
                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                return;
            }
            
            //if (plcMessage.EquipmentCode == "320662")
            //{
            //    _LogUtil.Debug($"{DateTime.Now}：挑选位【{plcMessage.EquipmentCode}】清理数据完成！");
            //}
            runLogInfo.RunLog = $"清除数据：【{plcMessage.EquipmentCode}】清除WCS-PLC数据成功";
            //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
            _LogUtil.Trace(runLogInfo.RunLog);
        }

    }
}
